function Poll()
{
    var url = 'server/poll.php?cmd=connections.get&id=' + DOM("id").value + '&nocache='+new Date(); 
    new Ajax.Request(url, {
    method: 'GET', 
    onSuccess: function(transport){
    	var jsonText = transport.responseText;
    	var cnxns = eval('(' + jsonText + ')');
    	//alert(jsonText);
    	for(var ndx = 0;ndx < cnxns.connections.length;ndx++)
    	{
    		cnxn = cnxns.connections[ndx];
    		//alert(cnxn.from);
    		PostsGet(cnxn.from,cnxn.last_update);
    	}
    	
    },
    onFailure: function(transport){ 
    	}
    }); 
}

function PostsGet(address, last_update)
{
    var url = 'server/poll.php?cmd=posts.get&from='+escape(address)+'&lastupdate='+escape(last_update)+'&id=' + DOM("id").value + '&nocache='+new Date(); 
    new Ajax.Request(url, {
    method: 'GET', 
    onSuccess: function(transport){
    	//if(transport.responseText.indexOf("test") > 0){DOM("link").value = transport.responseText};
    },
    onFailure: function(transport){ 
    	}
    }); 
}

function PostRefresh(id, channel, page)
{

    createCookie("channel",channel,365);
    
    if(DOM("divPosts"))
    {
	    var url = 'proxy.php';
	    var parms = 'id='+id+'&channel='+channel+'&cmd=posts&page='+page + '&nocache='+new Date();
	    
	    new Ajax.Request(url, {
	    parameters: parms,
	    method: 'POST', 
	    onSuccess: function(transport){
	    		DOM("divPosts").innerHTML = transport.responseText;
	    },
	    onFailure: function(transport){ 
	    	}
	    });
    } 
}

function PeopleListGet(page)
{

    DOM("pageNum").value = page;
	DOM("btnSearch").value = "submit";
        
	document.forms[0].submit();
	
	return true; 
}


function Profile(address, obj)
{
    var url = 'users/index.php?cmd=profile&address=' + address + '&nocache='+new Date(); 
    new Ajax.Request(url, {
    method: 'GET', 
    onSuccess: function(transport){
    	obj.innerHTML = transport.responseText; 
    },
    onFailure: function(transport){ 
    	}
    }); 
}





function loginCookieCheckStop()
{
   var id = readCookie("ombid");
   
   //Previous Login
   if(id) {
   		document.getElementById("id").value = id;
   }

}

function DOM(id)
{

	return document.getElementById(id);
}



function loginCookieCheck()
{
   var id = readCookie("ombid");
   
   //Previous Login
   if(DOM("id") && id) {
   		DOM("id").value = id;
   }
   else
   {
   		//New Login
   		if(DOM("id").value.length > 0)
   		{
   			createCookie("ombid",document.getElementById("id").value,365);
   			location.href="index.php?date=" + new Date;   			
   		}	
   		else
   		{
   			//Show Login
   			if(DOM("oid_form"))
   			{
   				DOM("oid_form").style.display = "block";
   				if(DOM("openmb_form"))
   				{
   				    DOM("openmb_form").style.display = "none";
   				}
   			}
   			else
   			{
   				location.href="index.php?date=" + new Date;
   			}
   		}
   }
}

function startup()
{
	//Poll every 20 seconds;
	setInterval("Poll()",1000 * 20);
	if(DOM("channel"))
	{
		if(readCookie("channel"))
		{	
		    DOM("channel").value = readCookie("channel");
		}
	}
	if(DOM("openmb_form"))
	{
		if(DOM("openmb_form").style.display != "none")
		{
			setTimeout('PostRefresh(DOM("id").value,DOM("channel").options[DOM("channel").selectedIndex].value,1)',100);
		}
	}

   
}

function statusCount()
{
    if(140 - DOM("status").value.length < 0)
    {
        DOM("status").value = DOM("status").value.substr(0,140);
    }
    DOM("statusLen").innerHTML = 140 - DOM("status").value.length;
}



function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function signout()
{
	eraseCookie("ombid");
	setTimeout('location.href="index.php?nocache="+new Date()',500);

}
